package com.lxmes.gs.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxmes.gs.mapper.WorkingHoursStandardMapper;
import com.lxmes.gs.domain.WorkingHoursStandard;
import com.lxmes.gs.service.IWorkingHoursStandardService;

/**
 * 工时标准Service业务层处理
 * 
 * @author wdf
 * @date 2023-04-24
 */
@Service
public class WorkingHoursStandardServiceImpl extends ServiceImpl<WorkingHoursStandardMapper,WorkingHoursStandard> implements IWorkingHoursStandardService{
    
    @Autowired
    private WorkingHoursStandardMapper workingHoursStandardMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询工时标准
     * 
     * @param id 工时标准主键
     * @return 工时标准
     */
    @Override
    public WorkingHoursStandard selectWorkingHoursStandardById(Long id){
        return workingHoursStandardMapper.selectById(id);
    }

    /**
     * 查询工时标准列表
     * 
     * @param workingHoursStandard 工时标准
     * @return 工时标准
     */
    @Override
    public List<WorkingHoursStandard> selectWorkingHoursStandardList(WorkingHoursStandard workingHoursStandard){
        return workingHoursStandardMapper.selectList(Wrappers.lambdaQuery(workingHoursStandard));
    }

    /**
     * 新增工时标准
     * 
     * @param workingHoursStandard 工时标准
     * @return 结果
     */
    @Override
    public int insertWorkingHoursStandard(WorkingHoursStandard workingHoursStandard){
        return workingHoursStandardMapper.insert(workingHoursStandard);
    }

    /**
     * 修改工时标准
     * 
     * @param workingHoursStandard 工时标准
     * @return 结果
     */
    @Override
    public int updateWorkingHoursStandard(WorkingHoursStandard workingHoursStandard){
        return workingHoursStandardMapper.updateById(workingHoursStandard);
    }

    /**
     * 批量删除工时标准
     * 
     * @param ids 需要删除的工时标准主键
     * @return 结果
     */
    @Override
    public int deleteWorkingHoursStandardByIds(Long[] ids){
        return workingHoursStandardMapper.deleteWorkingHoursStandardByIds(ids);
    }

    /**
     * 删除工时标准信息
     * 
     * @param id 工时标准主键
     * @return 结果
     */
    @Override
    public int deleteWorkingHoursStandardById(Long id){
        return workingHoursStandardMapper.deleteById(id);
    }
    @Override
    public String imporIteminfo(List<WorkingHoursStandard> workingHoursStandardList, boolean updateSupport) {
        if (StringUtils.isNull(workingHoursStandardList) || workingHoursStandardList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WorkingHoursStandard workingHoursStandard : workingHoursStandardList)
        {
            try
            {
                BeanValidators.validateWithException(validator, workingHoursStandard);
                this.insertWorkingHoursStandard(workingHoursStandard);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
